package com.bianmaba.activiti.beans.entity.authorization;

import com.bianmaba.spring.data.jpa.domain.annotations.QueryCacheable;
import com.bianmaba.spring.data.jpa.domain.entity.BaseEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源实体类
 *
 * @author cWX183898
 */
@DynamicUpdate(true)
@DynamicInsert(true)
@Entity
@Table(name = "System_Resource", uniqueConstraints = {@UniqueConstraint(columnNames = {"name", "url"})})
//此注解重新定义为是否开启查询缓存
@QueryCacheable
//此注解表示此类支持二级缓存，使用默认缓存（由缓存管理器根据ehcache.xml中的defaultCache自动创建）
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Resource extends BaseEntity {
    private static final long serialVersionUID = -4944044646898487415L;

    /**
     * 资源名称
     */
    @Column(length = 512, nullable = false)
    private String name;

    /**
     * 资源URL地址
     */
    @Column(length = 512, nullable = false)
    private String url;

    /**
     * 资源URL描述
     */
    @Column(length = 1024)
    private String description;

    /**
     * 资源所属角色列表
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "System_Resource_ref_Role", joinColumns = @JoinColumn(name = "resource_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<Role> roles = new ArrayList<Role>(0);

    public String getUrl() {
        return url;
    }


    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void addRole(Role role) {
        if (this.getRoles() == null) {
            this.setRoles(new ArrayList<Role>(0));
        }
        this.getRoles().add(role);
    }


}
